package com.sunseen.intelligencebus

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ProgressBar
import android.widget.ScrollView
import android.widget.Toast
import androidx.databinding.ViewDataBinding
import com.avos.avoscloud.*
import com.sunseen.intelligencebus.base.AbsBindingFragment
import com.sunseen.intelligencebus.common.Preference
import com.sunseen.intelligencebus.databinding.FragmentRegisterBinding
import com.sunseen.intelligencebus.user.Account

class RegisterFragment : AbsBindingFragment() {

    private var isLogin: Boolean by Preference(Preference.LOGIN_STATE, false)

    lateinit var formView: ScrollView
    lateinit var progressView: ProgressBar

    override fun setLayout() = R.layout.fragment_register

    override fun onBindView(viewDataBinding: ViewDataBinding,bundle: Bundle?) {
        val binding = viewDataBinding as FragmentRegisterBinding
        binding.toolBar.setNavigationOnClickListener { pop() }
        formView = binding.registerForm
        progressView = binding.registerProgress
        binding.usernameRegisterButton.setOnClickListener {
            val userName = binding.username.text
            val passWord = binding.password.text
            val passWordConfirm = binding.passwordConfirm.text

            if (userName.isNullOrBlank()) {
                Toast.makeText(context, "请输入用户名", Toast.LENGTH_SHORT).show()
            } else if (passWord.isNullOrBlank()) {
                Toast.makeText(context, "请输入密码", Toast.LENGTH_SHORT).show()
            } else if (passWordConfirm.isNullOrBlank()) {
                Toast.makeText(context, "请再次输入密码", Toast.LENGTH_SHORT).show()
            } else if (passWord.toString() != passWordConfirm.toString()) {
                Toast.makeText(context, "前后密码不一致", Toast.LENGTH_SHORT).show()
            } else if (passWord.length < 4) {
                Toast.makeText(context, "密码长度需大于4位", Toast.LENGTH_SHORT).show()
            } else {
                showProgress(true)
                val user = AVUser()
                user.username = userName.toString()
                user.setPassword(passWord.toString())
                user.signUpInBackground(object : SignUpCallback() {
                    override fun done(p0: AVException?) {
                        if (p0 == null) {
                            //注册成功，注册的用户对象已赋值给当前用户，可用AVUser.getCurrentUser() 获得
                            //跳转到主界面
                            isLogin = true
                            createAccount()
                            Toast.makeText(context, "注册成功", Toast.LENGTH_SHORT).show()
                            startWithPopTo(MainFragment(), LoginFragment::class.java, true)
                        } else {
                            showProgress(false)
                            Toast.makeText(context, p0.message, Toast.LENGTH_SHORT).show()
                        }
                    }
                })
            }
        }
    }


    private fun showProgress(show: Boolean) {
        val shortAnimTime = resources.getInteger(android.R.integer.config_shortAnimTime)

        formView.visibility = if (show) View.GONE else View.VISIBLE
        formView.animate().setDuration(shortAnimTime.toLong()).alpha(
            (if (show) 0 else 1).toFloat()
        ).setListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                formView.visibility = if (show) View.GONE else View.VISIBLE
            }
        })
        progressView.visibility = if (show) View.VISIBLE else View.GONE
        progressView.animate().setDuration(shortAnimTime.toLong()).alpha(
            (if (show) 1 else 0).toFloat()
        ).setListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                progressView.visibility = if (show) View.VISIBLE else View.GONE
            }
        })
    }

    private fun createAccount() {
        val account = Account()
        account.name = AVUser.getCurrentUser().username
        account.setBalance(0.00)
        account.saveInBackground(object : SaveCallback() {
            override fun done(p0: AVException?) {
                if (p0 == null) {
                    Log.i("test", "创建了账户")
                } else {
                    Log.e("test", p0.message)
                }
            }
        })
    }

}